﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Text;

namespace TraditionalMalteseCuisine.App_Code.Objects
{
    [Serializable]
    public class ActivityLog : BaseTable
    {
        //*************************************************
        //                Class Members
        //*************************************************
        #region Class Members
        public const string TABLE_NAME = "ActivityLog";
        public const string VIEW_NAME_ALL_ACTIVITY_LOG_LAST_LOGIN = "vw" + TABLE_NAME + "LastLogin";
        public const string VIEW_NAME_ACTIVITY_LOG_TOTAL_DAILY_ACTIVITY = "vw" + TABLE_NAME + "TotalDailyActivity";

        public const string LOGIN_ACTION = "LOGIN";

        public const string MEMBER_ID_TAG = "member_" + SiteMember.ID_TAG;
        public const string ACTIVITY_TAG = "activity";
        public const string DESCRIPTION = "description";
        public const string ACTIVITY_DATE = "activity_date";

        public const string TOTAL_ACTIVITY = "TotalActivity";

        private int mMemberID = -1;
        private string mActivity = string.Empty;
        private string mDescription = string.Empty;
        private DateTime mActivityDate = DateTime.Now;
        #endregion
        //*************************************************


        //*************************************************
        //                 Constructor
        //*************************************************
        #region Constructor
        public ActivityLog() : this(-1, string.Empty, string.Empty) { }

        public ActivityLog(int memberID, string activity, string description)
        {
            MemberID = memberID;
            Activity = activity;
            Description = description;
        }
        #endregion
        //*************************************************


        //*************************************************
        //                   Methods
        //*************************************************
        #region Methods
        protected override string GetInsertScriptUsingParameters()
        {
            StringBuilder insertScript = new StringBuilder();

            insertScript.Append("INSERT INTO ");
            insertScript.AppendFormat("{0} ", TABLE_NAME);
            insertScript.AppendFormat("([{0}], [{1}], [{2}]) ", MEMBER_ID_TAG, ACTIVITY_TAG, DESCRIPTION);
            insertScript.Append("VALUES ");
            insertScript.AppendFormat("(@{0}, @{1}, @{2}) ", MEMBER_ID_TAG, ACTIVITY_TAG, DESCRIPTION);

            return insertScript.ToString();
        }

        public override OleDbCommand GetInsertCommand()
        {
            OleDbCommand insertCommand = new OleDbCommand();

            insertCommand.CommandType = CommandType.Text;
            insertCommand.CommandText = GetInsertScriptUsingParameters();

            insertCommand.Parameters.AddWithValue(string.Format("@{0}", MEMBER_ID_TAG), MemberID);
            insertCommand.Parameters.AddWithValue(string.Format("@{0}", ACTIVITY_TAG), Activity);
            insertCommand.Parameters.AddWithValue(string.Format("@{0}", DESCRIPTION), Description);

            return insertCommand;
        }

        public override BaseTable ReadDataRow(DataRow datarow)
        {
            //ID = int.Parse(datarow[ID_TAG].ToString());
            MemberID = int.Parse(datarow[MEMBER_ID_TAG].ToString());
            Activity = datarow[ACTIVITY_TAG].ToString();
            Description = datarow[DESCRIPTION].ToString();
            Activity_Date = DateTime.Parse(datarow[ACTIVITY_DATE].ToString());
            //InUse = bool.Parse(datarow[IN_USE_TAG].ToString());

            return this;
        }


        public override string ToString()
        {
            return string.Format("{0:dd/MMM/yyyy HH:mm:ss} - {1} - {2}", Activity_Date, Activity, Description);
        }
        #endregion
        //*************************************************


        //*************************************************
        //                  Properties
        //*************************************************
        #region Properties
        public int MemberID
        {
            get { return mMemberID; }
            set { mMemberID = value; }
        }

        public string Activity
        {
            get { return mActivity; }
            set { mActivity = value; }
        }

        public string Description
        {
            get { return mDescription; }
            set { mDescription = value; }
        }

        public DateTime Activity_Date
        {
            get { return mActivityDate; }
            set { mActivityDate = value; }
        }
        #endregion
        //*************************************************
    }
}
